<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/31
 * Time: 上午10:13
 */

namespace App\Modules\User\Services;


use App\Modules\User\Models\UserBill;

class UserBillService
{

    /**
     * 用户流水列表
     * @param $params[]
     *                  uid:用户ID 可选
     *                  type:流水类型
     *                  start_time:开始时间 可选
     *                  end_time:结束时间 可选
     * @param $isAll
     * @return mixed
     */
    public static function getList($params=[], $isAll = false) {
        return UserBill::getList($params, $isAll);
    }


    /**
     * 用户充值流水
     * @param $uid 用户ID
     * @param $amount 充值金额 单位元
     * @param $orderId 充值记录ID
     * @param String $taskId 任务ID
     * @param String $remark 充值说明
     * @return mixed
     */
    public static function addPayBill($uid, $amount, $orderId, $taskId = '', $remark = '') {
        $data = [
            'uid' => $uid,
            'amount' => $amount,
            'type' => UserBill::$_TYPE_Pay,
            'title' => '充值',
            'order_id' => $orderId,
            'remark' => $remark,
            'task_id' => $taskId,
        ];
        return UserBill::create($data);
    }

    /**
     * 用户提现流水
     * @param $uid 用户ID
     * @param $amount 提现金额 单位元
     * @param $payWithdrawCashId 提现记录ID
     * @param String $remark 充值说明
     * @return mixed
     */
    public static function addWithdrawBill($uid, $amount, $payWithdrawCashId, $remark = '') {
        $data = [
            'uid' => $uid,
            'amount' => $amount,
            'type' => UserBill::$_TYPE_Withdraw,
            'title' => '提现',
            'pay_withdraw_cash_id' => $payWithdrawCashId,
            'remark' => $remark,
        ];
        return UserBill::create($data);
    }

    /**
     * 用户退款流水
     * @param $uid 用户ID
     * @param $amount 退款金额 单位元
     * @param $taskId 任务ID
     * @param $orderId 充值订单ID
     * @param String $remark 充值说明
     * @return mixed
     */
    public static function addRefundBill($uid, $amount, $taskId, $orderId, $remark = '') {
        $data = [
            'uid' => $uid,
            'amount' => $amount,
            'type' => UserBill::$_TYPE_Refund,
            'title' => '任务退款',
            'task_id' => $taskId,
            'order_id' => $orderId,
            'remark' => $remark,
        ];
        return UserBill::create($data);
    }

    /**
     * 用户创建任务需要的任务奖金
     * @param $uid 用户ID,任务创建者用户ID
     * @param $amount 任务支付佣金
     * @param $taskId 任务ID
     * @param $taskTitle 任务标题
     * @param String $remark 说明
     * @return mixed
     */
    public static function addRewardOutBill($uid, $amount, $taskId, $taskTitle, $remark = '') {
        $data = [
            'uid' => $uid,
            'amount' => $amount,
            'type' => UserBill::$_TYPE_RewardOut,
            'title' => $taskTitle,
            'remark' => $remark,
            'task_id' => $taskId,
        ];
        return UserBill::create($data);
    }

    /**
     * 用户获取到任务佣金
     * @param $uid 用户ID，获得佣金用户ID
     * @param $amount 任务获取佣金
     * @param $taskId 任务ID
     * @param $taskTitle 任务标题
     * @param String $remark 说明
     * @return mixed
     */
    public static function addRewardInBill($uid, $amount, $taskId, $taskTitle, $remark = '') {
        $data = [
            'uid' => $uid,
            'amount' => $amount,
            'type' => UserBill::$_TYPE_RewardIn,
            'title' => $taskTitle,
            'remark' => $remark,
            'task_id' => $taskId,
        ];
        return UserBill::create($data);
    }

    /**
     * 指定用户获取到的总佣金
     * @param $uid
     * @return mixed
     */
    public static function getRewardInCount($uid) {
        return UserBill::where('type', UserBill::$_TYPE_RewardIn)
            ->where('uid', $uid)
            ->sum('amount');
    }

    /**
     * 获取指定用户打赏总额
     * @param $uid
     * @return mixed
     */
    public static function getRewardOutCount($uid) {
        return UserBill::where('type', UserBill::$_TYPE_RewardOut)
            ->where('uid', $uid)
            ->sum('amount');
    }
}